//
//  ViewController.m
//  YYCache
//
//  Created by GR on 2017/10/25.
//  Copyright © 2017年 Harry. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import <YYCache.h>
#import <FMDB.h>

@interface ViewController ()
@property (strong, nonatomic) NSMutableArray *randomNoArray;
@property (strong, nonatomic) NSMutableArray *students;
@end

@implementation ViewController
{
    FMDatabase *_dataBase;
    YYCache *_cache;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _cache = [YYCache cacheWithPath:@"/Users/apple/Desktop/DataBase/yycache"];
    
    [self openDataBase];
    [self students];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSTimeInterval start = CFAbsoluteTimeGetCurrent();
//    [_cache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
//        NSLog(@"%f", removedCount * 1.f / totalCount);
//    } endBlock:^(BOOL error) {
//
//    }];
//    [self insertStudents];
//    [_cache setObject:self.students forKey:@"students"];
    for (Student *st in self.students) {
        NSString *key = [NSString stringWithFormat:@"%zd", st.studentNo];
        [_cache setObject:st forKey:key withBlock:^{
//            NSLog(@"%@", [NSThread currentThread]);
//            NSLog(@"完成");
        }];
    }
    NSTimeInterval end = CFAbsoluteTimeGetCurrent();
    NSLog(@"cost -- %f", end - start);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_cache removeObjectForKey:@"students"];
}

- (void)insertStudents
{
    NSMutableString *sqls = [NSMutableString stringWithCapacity:0];
    for (Student *st in self.students) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_student (name, age, score) VALUES ('%@', %zd, %f);", st.name, st.age, st.score];
        [sqls appendString:sql];
    }
    
    [_dataBase beginTransaction];
//    NSTimeInterval start = CFAbsoluteTimeGetCurrent();
    BOOL success = [_dataBase executeStatements:sqls];
//    NSTimeInterval end = CFAbsoluteTimeGetCurrent();
    [_dataBase commit];
//    NSLog(@"cost -- %f", end - start);
    if (success) {
        NSLog(@"插入成功");
    }
    else {
        NSLog(@"插入失败");
    }
    
}

- (void)openDataBase
{
    _dataBase = [[FMDatabase alloc] initWithPath:@"/Users/apple/Desktop/DataBase/student.sqlite"];
    if ([_dataBase open]) {
        [self createStudentTable];
    }
}

- (void)createStudentTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS t_student(no INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER, score REAL DEFAULT 60.0)";
    if ( [_dataBase executeUpdate:sql] ) {
        NSLog(@"创建成功");
    }
    else {
        NSLog(@"创建失败");
    }
}


- (Student *)randomStudent
{
    NSInteger no = [self randomNo];
    NSInteger age = arc4random() % 9 + 10;
    float score = arc4random() % 100;
    NSString *name = [self randomName];
    
    return [Student studentWithNo:no name:name age:age score:score];
}

- (NSInteger)randomNo
{
    NSInteger randomIndex = arc4random() % 10000;
    NSInteger no = [self.randomNoArray[randomIndex] integerValue];
    [self.randomNoArray removeObjectAtIndex:randomIndex];
    return no;
}

- (NSString *)randomName
{
    NSArray *chars = @[ @"张", @"李", @"王", @"连", @"馨", @"周", @"亮", @"田", @"艾", @"杨", @"明", @"敏"];
    NSInteger count = chars.count;
    NSMutableString *name = [NSMutableString stringWithCapacity:0];
    for (int i = 0; i < 2; i++) {
        [name appendString:chars[arc4random() % (count - 1)]];
    }
    
    return name;
}


- (NSMutableArray *)students {
    
    if (!_students) {
        _students = [NSMutableArray arrayWithCapacity:0];
        for (int i = 0; i < 10000; i++) {
            [_students addObject:[self randomStudent]];
        }
    }
    return _students;
}


- (NSMutableArray *)randomNoArray {
    
    if (!_randomNoArray) {
        _randomNoArray = [NSMutableArray arrayWithCapacity:100000];
        for (int i = 1; i <= 100000; i++) {
            [_randomNoArray addObject:@(i)];
        }
    }
    return _randomNoArray;
}

@end
